python集合set用法详解(创建、增加、删除、复制、查找、合并、判断、差集、交集、对称差集) 您所在的位置:网站首页 交集函数 python python集合set用法详解(创建、增加、删除、复制、查找、合并、判断、差集、交集、对称差集)

python集合set用法详解(创建、增加、删除、复制、查找、合并、判断、差集、交集、对称差集)

2024-07-06 06:51| 来源: 网络整理| 查看: 265

1、创建集合 1.1 可变集合  

创建集合使⽤ {} 或 set() , 但是如果要创建空集合只能使⽤ set() ,因为 {} ⽤来创建空字典。

示例代码:

s1 = {10, 20, 30, 40, 50} print(s1) s2 = {10, 30, 20, 10, 30, 40, 30, 50} print(s2) s3 = set('abcdefg') # 生成了无序集合 print(s3) s4 = set() print(type(s4)) # set s5 = {} print(type(s5)) # dict

运行结果:

特点: 集合可以去掉重复数据; 集合数据是⽆序的,故不⽀持下标 1.2 不可变集合(frozenset)

直接使用frozenset,不需要导入。不可变集合不许对其进行修改,不存在update等修改的方法。

# 不可变集合 set_data_1 = frozenset('abcdef') print(set_data_1) # frozenset({'e', 'f', 'c', 'a', 'd', 'b'}) 2、集合常见的操作方法 2.1 增加数据(add、update)

add():将元素添加到集合中。

注意:如果该元素已经存在,则add()方法不会添加该元素。

示例代码:

s1 = {10, 20} s1.add(100) s1.add(10) print(s1) # {100, 10, 20}

运行结果:

 

        因为集合有去重功能,所以,当向集合内追加的数据是当前集合已有数据的话,则不进⾏任何操作。

update():用于修改当前集合,可以添加新的元素或集合到当前集合中,如果添加的元素在集合中已存在,则该元素只会出现一次,重复的会忽略。

示例代码1:

s1 = {10, 20} # s1.update(100) # 报错 【不能直接放int型数字】 s1.update([100, 200]) s1.update('abc') print(s1)

运行结果:

示例代码2:

a = {"apple", "banana", "cherry"} b = {"google", "microsoft", "apple"} a.update(b) print(a)

运行结果:

2.2 删除数据(remove、discard、pop、clear)

remove(),删除集合中的指定数据,如果数据不存在则报错

示例代码:

s1 = {10, 20} s1.remove(10) print(s1) s1.remove(10) # 报错 print(s1) 运行结果:

discard(),删除集合中的指定数据,如果数据不存在也不会报错。

注意:这个方法与remove()方法不同,因为如果指定的元素不存在,remove()方法会引发错误,而discard()方法则不会。

示例代码:

s1 = {10, 20} s1.discard(10) print(s1) s1.discard(10) print(s1) 运行结果:

pop(),随机删除集合中的某个数据,并返回这个数据。

注意:此方法返回删除的集合元素,没有参数。当集合为空时再次使用pop方法会报错!

示例代码1:

s1 = {10, 20, 30, 40, 50} del_num = s1.pop() print(del_num) print(s1)

运行结果:

示例代码2:

s1 = set() print(s1) del_num = s1.pop() print(del_num) print(s1)

运行结果:

clear()方法删除集合中的所有元素。

示例代码:

x = {"a", "b", "c"} print(x) x.clear() print(x)

运行结果:

2.3 查找数据(in、not in) in:判断数据在集合序列 not in:判断数据不在集合序列 示例代码: s1 = {10, 20, 30, 40, 50} print(10 in s1) print(10 not in s1)

示例代码:

2.4 合并数据(union)

union() 方法返回两个集合的并集,即包含了所有集合的元素,重复的元素只会出现一次。

语法结构:

set.union(set1, set2...)

参数:

set1 -- 必需,合并的目标集合set2 -- 可选,其他要合并的集合,可以多个,多个使用逗号 , 隔开。return 一个新集合。

示例代码1:  【合并两个集合,重复元素只会出现一次】【注意:集合合并后不会改变原集合】

x = {"apple", "banana", "cherry"} y = {"zhangsan", "lisi", "apple"} print(x) print(y) result1 = x.union(y) print(x) print(y) result2 = set.union(x, y) print(x) print(y) print(result1) print(result2)

运行结果:

 示例代码2:  【合并多个集合】

x = {"a", "b", "c"} y = {"f", "d", "a"} z = {"c", "d", "e"} result1 = x.union(y, z) result2 = set.union(x, y, z) result3 = set.union(y, y, z) print(result1) print(result2) print(result3)

运行结果:

2.5 判断数据(issubset、isdisjonint、issuperset)

issubset() 方法用于判断集合的所有元素是否都包含在指定集合中,如果是则返回 True,否则返回 False。

语法如下:

set.issubset(set) set -- 必需,要比查找的集合返回布尔值,如果都包含返回 True,否则返回 False

示例代码1:

x = {"a", "b", "c"} y = {"f", "e", "d", "c", "b", "a"} z = x.issubset(y) print(z)

运行结果:

示例代码2:

x = {"a", "b", "c"} y = {"f", "e", "d", "c", "b"} z = x.issubset(y) print(z)

运行结果:

isdisjoint()方法:如果两个集合中都没有相同的元素,则返回True,否则返回False。

示例代码:

a = {"apple", "banana", "cherry"} b = {"google", "microsoft", "facebook"} c = {"aa", "bb"} d = {"bb", "cc"} x = a.isdisjoint(b) y = set.isdisjoint(a, b) print(x) print(y) m = c.isdisjoint(d) n = set.isdisjoint(c, d) print(m) print(n)

运行结果:

issuperset():如果指定集合中的所有元素都存在于原集合中,则返回True,否则返回False。

示例代码:

a = {"f", "e", "d", "c", "b", "a"} b = {"a", "b", "c"} c = {"a", "d"} x = a.issuperset(b) y = set.issuperset(a, b) print(x) print(y) print(a) print(b) z = b.issuperset(c) print(z)

运行结果:

2.6 复制数据(copy)

copy()方法复制该集合

示例代码:

x = {"a", "b", "c"} print(x) y = x.copy() print(y) print(type(y))

运行结果:

2.7 差集数据(difference、difference_update)

difference()方法返回一个包含两个集合之间的差异的集合。就是返回的集合包含仅存在于第一个集合中而不存在于两个集合中的元素。

示例代码1:   【返回一个集合,其中包含只存在于集合x中而不存在于集合y中的元素】

x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.difference(y) print(z)

运行结果:

示例代码2:【反转示例1。返回一个集合,其中包含只存在于集合y中而不存在于集合x中的元素】

x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = y.difference(x) print(z)

运行结果:

difference_update()方法删除两个集中存在的元素。

注意:difference_update()方法与difference()方法不同,因为difference()方法返回一个新的集合,不包含不需要的元素,而difference_update()方法从原始集合中删除不需要的元素。

示例代码:  【删除两个集合中都存在的元素】

x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.difference_update(y) print(x)

运行结果:

2.8 交集数据(intersection、intersection_update)

intersection()方法用于返回两个或更多集合中都包含的元素,即交集。返回的集合仅包含两个集合中都存在的元素,或者如果使用两个以上的集合进行比较,则包含所有集合中的元素。

语法结构:

set.intersection(set1, set2 ... etc)

示例代码1:  【返回一个包含同时存在于x和y中的元素的集合:】

a = {"apple", "banana", "cherry"} b = {"google", "microsoft", "apple"} x = a.intersection(b) y = set.intersection(a, b) print(x) print(y)

运行结果:

示例代码2:【比较多个集合,返回一个集合,其中包含所有集合中存在的元素:】

a = {"apple", "banana", "cherry", "aa"} b = {"google", "microsoft", "apple", "aa"} c = {"apple", "pear", "aa"} d = {"banana", 'water'} x = a.intersection(b, c) y = set.intersection(a, b, c) print(x) print(y) # m = a.intersection(a, b, d) m = a.intersection(b, d) n = set.intersection(a, b, d) print(m) print(n) print(a) print(b) print(c) print(d)

运行结果:

intersection_update()方法删除两个集合中不存在的元素(如果在两个以上集合之间进行比较,则删除所有集合中不存在的元素),即计算交集。

注意:intersection_update()方法与intersection()方法不同,因为intersection()方法返回一个新的集合,不包含不需要的元素,而intersection_update()方法则从原始集合中删除不需要的元素。

语法结构:

set.intersection_update(set1, set2 ... etc)

示例代码1:   【删除x和y都不存在的元素】

x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} a = x.intersection_update(y) print(x) print(y) print(a)

运行结果:

示例代码2:

a = {"apple", "banana", "cherry", "aa"} b = {"google", "microsoft", "apple", "aa"} c = {"apple", "pear", "aa"} d = {"google", 'water'} print(a) print(b) print(c) print(d) x = a.intersection_update(b, c) # 注意,必须是a,b,c共同的交集 set.intersection_update(b, c, d) set.intersection_update(c, d) print(x) print(a) print(b) print(c) print(d)

运行结果:

2.9 对称差集数据(symmetric_difference、sysmmetric_diffence_update)

symmetric_difference()方法返回一个集合,其中包含两个集合中的所有元素,但不包含两个集合中都存在的元素。就是返回的集合包含两个集合中都不存在的元素。

示例代码:

a = {"apple", "banana", "cherry"} b = {"google", "microsoft", "apple"} x = a.symmetric_difference(b) y = set.symmetric_difference(a, b) print(x) print(y)

运行结果:

symmetric_difference_update()方法移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中。

示例代码:

a = {"apple", "banana", "cherry"} b = {"google", "microsoft", "apple"} x = a.symmetric_difference_update(b) # set.symmetric_difference_update(a, b) # 注意,这行代码不能同时和上面那行代码同时运行,否则影响输出结果 print(x) print(a) print(b)

运行结果:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有